/************************************************************************************/
/*  Program name CAPFNDEXT_SECURITY_PROFILE.sql                                     */
/*  Creater:  CHAN KA KEUNG BILLY (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 09, MAY 2006 CHAN KA KEUNG BILLY     First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDEXT_SECURITY_PROFILE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFND_SECURITY_PROFILE.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT	* FROM PER_SECURITY_PROFILES
	WHERE SUBSTR(SECURITY_PROFILE_NAME,1,4) = lv_NGO
	OR SUBSTR(SECURITY_PROFILE_NAME,5,4) = lv_NGO;

BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.SECURITY_PROFILE_ID           ||CHR(9)||
				C1.BUSINESS_GROUP_ID             ||CHR(9)||
				C1.POSITION_ID                   ||CHR(9)||
				C1.ORGANIZATION_ID               ||CHR(9)||
				C1.POSITION_STRUCTURE_ID         ||CHR(9)||
				C1.ORGANIZATION_STRUCTURE_ID     ||CHR(9)||
				C1.INCLUDE_TOP_ORGANIZATION_FLAG ||CHR(9)||
				C1.INCLUDE_TOP_POSITION_FLAG     ||CHR(9)||
				C1.SECURITY_PROFILE_NAME         ||CHR(9)|| 
				C1.VIEW_ALL_APPLICANTS_FLAG      ||CHR(9)||
				C1.VIEW_ALL_EMPLOYEES_FLAG       ||CHR(9)||
				C1.VIEW_ALL_FLAG                 ||CHR(9)||
				C1.VIEW_ALL_ORGANIZATIONS_FLAG   ||CHR(9)||
				C1.VIEW_ALL_PAYROLLS_FLAG        ||CHR(9)||
				C1.VIEW_ALL_POSITIONS_FLAG       ||CHR(9)||
				C1.INCLUDE_EXCLUDE_PAYROLL_FLAG  ||CHR(9)||
				C1.REPORTING_ORACLE_USERNAME     ||CHR(9)||
				C1.REQUEST_ID                    ||CHR(9)||
				C1.PROGRAM_APPLICATION_ID        ||CHR(9)||
				C1.PROGRAM_ID                    ||CHR(9)||
				C1.PROGRAM_UPDATE_DATE           ||CHR(9)||
				C1.LAST_UPDATE_DATE              ||CHR(9)||
				C1.LAST_UPDATED_BY               ||CHR(9)||
				C1.LAST_UPDATE_LOGIN             ||CHR(9)||
				C1.CREATED_BY                    ||CHR(9)||
				C1.CREATION_DATE                 ||CHR(9)||
				C1.ALLOW_GRANTED_USERS_FLAG      ||CHR(9)||
				C1.RESTRICT_BY_SUPERVISOR_FLAG   ||CHR(9)||
				C1.SUPERVISOR_LEVELS             ||CHR(9)||
				C1.EXCLUDE_SECONDARY_ASGS_FLAG   ||CHR(9)||
				C1.EXCLUDE_PERSON_FLAG           ||CHR(9)||
				C1.NAMED_PERSON_ID               ||CHR(9)||
				C1.CUSTOM_RESTRICTION_FLAG       ||CHR(9)||
				C1.RESTRICTION_TEXT              ||CHR(9)||
				C1.EXCLUDE_BUSINESS_GROUPS_FLAG  ||CHR(9)||
				C1.ORG_SECURITY_MODE             ||CHR(9)||
				C1.VIEW_ALL_CWK_FLAG             ||CHR(9)||
				C1.VIEW_ALL_CONTACTS_FLAG        
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Concurrent Program for this NGO';
		END IF;
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/